package com.example.demo.validation;

import com.example.demo.annotation.CheckDate;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author lyc
 * @date 2019/6/3.
 */
public class CheckDateConstraintValidator implements ConstraintValidator<CheckDate, Date> {
   public void initialize(CheckDate constraint) {
   }

   public boolean isValid(Date obj, ConstraintValidatorContext context) {
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
      boolean flag = (obj == null);
      try {
         simpleDateFormat.format(obj);
      }catch (Exception e){
         return false;
      }
      return !flag;
   }
}